<?php
/**
 * Created by PhpStorm.
 * User: zhengzifan
 * Date: 2017/3/16
 * Time: 10:45
 */

namespace app\admin\controller;
use think\Controller;

class CommonController extends Controller{
    public function _initialize(){
		$power_ids='';
        $admin = session('admin');
        if(!$admin){
            $this->redirect('Public/login');
        }

        //权限
        $powerWhere['is_used'] = '1';
        $powerWhere['parent_id'] = 0;
        if($admin['role_id'] != 1){
            //如果不是超级管理员，则查询 角色权限表
            $power_ids = db('RbacPowerRole')->where(['role_id'=>$admin['role_id']])->column('power_id');
            if(!empty($power_ids)){
                $power_ids = implode(',',$power_ids);
                $powerWhere['id'] = ['in',$power_ids];
            }
        }
        //处理菜单
        $powers = db('RbacPower')->where($powerWhere)->order('sort asc')->select();
        foreach($powers as &$power){
            $powerSonWhere['is_used'] = '1';
            $powerSonWhere['parent_id'] = $power['id'];
            if($power_ids){
                $powerSonWhere['id'] = ['in',$power_ids];
            }
            $power['son'] = db('RbacPower')->where($powerSonWhere)->order('sort asc')->select();
        }
        $this->assign('powers',$powers);
        //验证权限
        $powerAction = ['index'];//需要验证的操作名，如需验证子页面权限，注释该行并删除下方if条件里的in_array判断
        if(CONTROLLER_NAME != 'Index' && in_array(ACTION_NAME,$powerAction) && $admin['role_id'] != 1){
            if(!db('RbacPower')->where(['id'=>['in',$power_ids],'controller'=>$controller_name,'action'=>ACTION_NAME])->find()){
                $this->error('权限不足！');
            }
        }
        checkOrders();
    }

    /* 
        获取栏目列表
    */
    public function getOrderArticleCategory(){
        $categorys = db('ArticleCategory')->where(['id'=>['>',0]])->select();
        
        $list = array();
        $deepPath = 1;
        foreach($categorys as $item){
            if($item['parent_id'] == 0){
                $nitem = ['id'=>$item['id'],'parent_id'=>$item['parent_id'],'name'=>$item['name'],'deepPath'=>$deepPath];
                array_push($list,$nitem);
                $nodes = $this->getArticleCategoryChild($item['id'],$deepPath,$categorys);
                if($nodes){
                    foreach($nodes as $node){
                        array_push($list,$node);
                    }
                }
            }
        }

        return $list;
    }

    public function getArticleCategoryChild($parent_id,$deepPath,$categorys){
        $nodePath = $deepPath + 1;
        $nlist = array();
        foreach($categorys as $item){
            if($item['parent_id'] == $parent_id){
                $nitem = ['id'=>$item['id'],'parent_id'=>$item['parent_id'],'name'=>$item['name'],'deepPath'=>$nodePath];
                array_push($nlist,$nitem);
                $nodes = $this->getArticleCategoryChild($item['id'],$nodePath,$categorys);
                if($nodes){
                    foreach($nodes as $node){
                        array_push($nlist,$node);
                    }
                }
            }
        }
        return $nlist;
    }

}